package cz.kfad.ofrss.db;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.database.Cursor;
import cz.kfad.ofrss.model.Category;
import cz.kfad.ofrss.model.Channel;
import cz.kfad.ofrss.model.RssFeed;
import cz.kfad.ofrss.model.RssFeedShort;

public class CursorToListConverterUtil {

    public static List<RssFeed> toRssList(final Cursor cursor) {
        final ArrayList<RssFeed> rssFeeds = new ArrayList<RssFeed>();

        if (cursor.moveToFirst()) {
            final int indexId = cursor.getColumnIndex(DbConstants.COLUMN_RSS_FEED_ID);
            final int indexText = cursor.getColumnIndex(DbConstants.COLUMN_RSS_FEED_TEXT);
            final int indexTitle = cursor.getColumnIndex(DbConstants.COLUMN_RSS_FEED_TITLE);
            final int indexRead = cursor.getColumnIndex(DbConstants.COLUMN_RSS_FEED_READ);
            final int indexChannel = cursor.getColumnIndex(DbConstants.COLUMN_RSS_FEED_CHANNEL_ID);
            final int indexPubDate = cursor.getColumnIndex(DbConstants.COLUMN_RSS_FEED_PUBDATE);
            final int indexURL = cursor.getColumnIndex(DbConstants.COLUMN_RSS_FEED_URL);
            final int indexContent = cursor.getColumnIndex(DbConstants.COLUMN_RSS_FEED_CONTENT);
			final int indexContentAttempt = cursor.getColumnIndex(DbConstants.COLUMN_RSS_FEED_CONTENT_ATTEMPTS);
            do {
                final RssFeed rssFeed = new RssFeed();
                rssFeed.setId(cursor.getInt(indexId));
                rssFeed.setText(cursor.getString(indexText));
                rssFeed.setTitle(cursor.getString(indexTitle));
                rssFeed.setUrl(cursor.getString(indexURL));
                rssFeed.setRead(cursor.getInt(indexRead) == 1);
                rssFeed.setChannelId(cursor.getInt(indexChannel));
                rssFeed.setDate(new Date(cursor.getLong(indexPubDate)));
				rssFeed.setContent(cursor.getString(indexContent));
				rssFeed.setContentAttempts(cursor.getInt(indexContentAttempt));
                rssFeeds.add(rssFeed);
            } while (cursor.moveToNext());
        }
        cursor.close();
        return rssFeeds;
    }

    public static List<Category> toCategoryList(final Cursor cursor) {
        final List<Category> categories = new ArrayList<Category>();

        if (cursor.moveToFirst()) {
            final int indexId = cursor.getColumnIndex(DbConstants.COLUMN_CATEGORY_ID);
            final int indexName = cursor.getColumnIndex(DbConstants.COLUMN_CATEGORY_NAME);
            final int indexKey = cursor.getColumnIndex(DbConstants.COLUMN_CATEGORY_KEY);
            final int indexTotalFeeds = cursor.getColumnIndex(DbConstants.COLUMN_CATEGORY_TOTAL_FEEDS);
            final int indexUnreadFeeds = cursor.getColumnIndex(DbConstants.COLUMN_CATEGORY_UNREAD_FEEDS);
            do {
                final Category category = new Category();
                category.setId(cursor.getInt(indexId));
                category.setName(cursor.getString(indexName));
                category.setTotalFeeds(cursor.getInt(indexTotalFeeds));
                category.setUnreadFeeds(cursor.getInt(indexUnreadFeeds));
                category.setKey(cursor.getString(indexKey));
                categories.add(category);
            } while (cursor.moveToNext());
        }
        return categories;
    }

    public static List<Channel> toChannelList(final Cursor cursor) {
        final List<Channel> channels = new ArrayList<Channel>();

        if (cursor.moveToFirst()) {
            final int indexId = cursor.getColumnIndex(DbConstants.COLUMN_CHANNEL_ID);
            final int indexName = cursor.getColumnIndex(DbConstants.COLUMN_CHANNEL_NAME);
            final int indexUrl = cursor.getColumnIndex(DbConstants.COLUMN_CHANNEL_URL);
            final int indexTotalFeeds = cursor.getColumnIndex(DbConstants.COLUMN_CHANNEL_TOTAL_FEEDS);
            final int indexUnreadFeeds = cursor.getColumnIndex(DbConstants.COLUMN_CHANNEL_UNREAD_FEEDS);
            final int indexNewestFetched = cursor.getColumnIndex(DbConstants.COLUMN_CHANNEL_NEWEST_FETCHED);
            do {
                final Channel channel = new Channel();
                channel.setId(cursor.getInt(indexId));
                channel.setName(cursor.getString(indexName));
                channel.setUrl(cursor.getString(indexUrl));
                channel.setTotalFeeds(cursor.getInt(indexTotalFeeds));
                channel.setUnreadFeeds(cursor.getInt(indexUnreadFeeds));
                channel.setNewestFetched(new Date(Long.parseLong(cursor.getString(indexNewestFetched))));
                channels.add(channel);
            } while (cursor.moveToNext());
        }
        return channels;
    }

    public static List<Integer> toIntegerList(final Cursor cursor, final String key) {
        final List<Integer> integers = new ArrayList<Integer>();
        if (cursor.moveToFirst()) {
            final int indexInteger = cursor.getColumnIndex(key);
            do {
                integers.add(cursor.getInt(indexInteger));
            } while (cursor.moveToNext());
        }
        return integers;
    }

    public static String[] getColumnsForRssFeedShort() {
        return new String[]{DbConstants.COLUMN_RSS_FEED_ID, DbConstants.COLUMN_RSS_FEED_READ,
                DbConstants.COLUMN_RSS_FEED_TITLE, DbConstants.COLUMN_RSS_FEED_PUBDATE};
    }

    public static List<RssFeedShort> toRssShortList(final Cursor cursor) {
        final List<RssFeedShort> rssFeeds = new ArrayList<RssFeedShort>();

        if (cursor.moveToFirst()) {
            final int indexId = cursor.getColumnIndex(DbConstants.COLUMN_RSS_FEED_ID);
            final int indexTitle = cursor.getColumnIndex(DbConstants.COLUMN_RSS_FEED_TITLE);
            final int indexRead = cursor.getColumnIndex(DbConstants.COLUMN_RSS_FEED_READ);
            final int indexPubDate = cursor.getColumnIndex(DbConstants.COLUMN_RSS_FEED_PUBDATE);
            do {
                final RssFeedShort rssFeed = new RssFeedShort();
                rssFeed.setId(cursor.getInt(indexId));
                rssFeed.setTitle(cursor.getString(indexTitle));
                rssFeed.setRead(cursor.getInt(indexRead) == 1);
                rssFeed.setDate(new Date(cursor.getLong(indexPubDate)));
                rssFeeds.add(rssFeed);
            } while (cursor.moveToNext());
        }
        cursor.close();
        return rssFeeds;
    }
}
